package com.vn.rainbow.exoalarm.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import com.vn.rainbow.exoalarm.R;
import com.vn.rainbow.exoalarm.listener.DaySelectedListener;
import com.vn.rainbow.exoalarm.model.Point;

public class DaySelectedView extends BaseView {

	private Paint mCircleStroke, mCirlceFill, mTextPaint;
	private float mRadius;
	private int color;

	private String[] day;

	private Point[] mPoints;

	private float mPadding;

	private DaySelectedListener mDaySelectedListener;

	public DaySelectedView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	protected void init() {
		// TODO Auto-generated method stub
		color = getResources().getColor(R.color.color_theme);
		mRadius = getResources().getDimension(R.dimen.radius_day);

		day = new String[] { "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" };
		Log.d("Kai", "radius  " + mRadius);

		mCircleStroke = initPaint();
		mCircleStroke.setStyle(Style.STROKE);
		mCircleStroke.setColor(color);

		mCirlceFill = initPaint();
		mCirlceFill.setStyle(Style.FILL);
		mCirlceFill.setColor(color);

		mTextPaint = initPaint();
		mTextPaint.setColor(color);
		mTextPaint.setTextSize(10 * density);

		Rect bounds = new Rect();

		mPadding = 5 * density;

		w = (int) (mPadding * 6 + mRadius * 2 * 7 + mPadding);
		h = (int) (mRadius * 2 + mPadding);
		setMinimumWidth((int) w);
		setMinimumHeight((int) h);

		float baseX = mPadding / 2 + mRadius;
		float baseY = (int) (mRadius * 2 + mPadding) / 2;

		float gapX = mPadding + mRadius * 2;

		mPoints = new Point[7];

		for (int i = 0; i < day.length; i++) {
			mPoints[i] = new Point();
			float x = baseX + gapX * i;
			mPoints[i].x = x;
			mPoints[i].y = baseY;
			mPoints[i].text = day[i];

			mTextPaint.getTextBounds(day[i], 0, 3, bounds);
			float xPos = (int) (x - bounds.width() / 2);
			float yPos = (int) (baseY - ((mTextPaint.descent() + mTextPaint
					.ascent()) / 2));

			mPoints[i].xText = xPos;
			mPoints[i].yText = yPos;
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:

			break;

		case MotionEvent.ACTION_UP:
			for (int i = 0; i < 7; i++) {
				Point p = mPoints[i];
				if (kissCircle(event, p.x, p.y)) {
					p.touch = !p.touch;

					invalidate();
					if (mDaySelectedListener != null)
						mDaySelectedListener.onDaySelected(i);
					break;
				}
			}
			break;
		}
		return super.onTouchEvent(event);
	}

	private boolean kissCircle(MotionEvent e, float x, float y) {
		float mCurrX = e.getX();
		float mCurrY = e.getY();

		if (Math.sqrt(Math.pow(mCurrX - x, 2) + Math.pow(mCurrY - y, 2)) <= mRadius) {

			return true;
		}
		return false;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

		for (int i = 0; i < mPoints.length; i++) {
			Point point = mPoints[i];
			if (point.touch) {

				canvas.drawCircle(point.x, point.y, mRadius, mCirlceFill);
				mTextPaint.setColor(Color.WHITE);
				canvas.drawText(point.text, point.xText, point.yText,
						mTextPaint);
			} else {
				canvas.drawCircle(point.x, point.y, mRadius, mCircleStroke);
				mTextPaint.setColor(color);
				canvas.drawText(point.text, point.xText, point.yText,
						mTextPaint);
			}

		}
	}

	public DaySelectedListener getDaySelectedListener() {
		return mDaySelectedListener;
	}

	public void setDaySelectedListener(DaySelectedListener mDaySelectedListener) {
		this.mDaySelectedListener = mDaySelectedListener;
	}

}
